import React, { Component } from 'react';
import { Modal, Form, message, Radio, Button, Icon, Input, Col, Row, InputNumber } from 'antd';

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
export default class SetBright extends Component {
  state = {
    brightnessVal: 1,
  };

  onChange = brightnessVal => {
    this.setState({
      brightnessVal,
    });
  };
  handleSet = () => {
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const { selectedRows, dispatch } = this.props;
        const { brightnessVal, setBrightnessMod } = values;
        const devIds = selectedRows.map(item => item.devId).join(',');

        dispatch({
          type: 'dev/setBright',
          payload: { devIds, setBrightnessMod, brightnessVal },
          callback: () => {
            this.props.handleModalVisible(false);
            message.success('设置亮度成功.');
          },
        });
      }
    });
  };

  render() {
    const { modalVisible, form, handleModalVisible } = this.props;
    return (
      <Modal
        title="设置亮度"
        visible={modalVisible}
        onOk={this.handleSet}
        onCancel={() => handleModalVisible(false)}
      >
        <Form>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="类型">
            {form.getFieldDecorator('setBrightnessMod', {
              initialValue: 0,
              rules: [{ required: true, message: '请选择类型...' }],
            })(
              <RadioGroup>
                <Radio value={0}>手动设置</Radio>
                <Radio value={1}>自动调节(需接入亮度传感器)</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="亮度值">
            {form.getFieldDecorator('brightnessVal', {
              initialValue: 10,
              rules: [{ required: true, message: '请选择类型...' }],
            })(<InputNumber min={1} max={64} />)}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}
